﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.DBUtility;
using MeTalker.Aide.Forms.Mode;

namespace MeTalker.Aide.Forms
{
    internal partial class frmCustomMessage : MeForm
    {
        List<CustomMsg> customs;
        List<CustomMsg> msgs;
        DataTable dt;
        public frmCustomMessage()
        {
            InitializeComponent();
            lbCustom.DisplayMember = "CustomName";
            lbCustom.ValueMember = "Id";
            BindData();
        }

        void BindData()
        {
            msgs = new List<CustomMsg>();
            customs = new List<CustomMsg>();
            dt = AccessHelper.GetCustomMsgs().Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                CustomMsg custom = new CustomMsg() { Id = int.Parse(row["Id"].ToString()), Content = row["Content"].ToString(), Time = row["WriterTime"].ToString(), CustomName = row["CustomName"].ToString(), ReceName = row["ReceName"].ToString() };
                 var ds = from temp in customs where temp.CustomName.Equals(custom.CustomName) select temp;
                 if (ds.Count() <= 0)
                 {
                     customs.Add(custom);
                 }
                 msgs.Add(custom);
            }
            lbCustom.DataSource = customs;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lbCustom_SelectedValueChanged(object sender, EventArgs e)
        {
            CustomMsg msg = lbCustom.SelectedItem as CustomMsg;
            if (msg != null)
            {
                string content = "";
                var ds = from temp in msgs where temp.CustomName.Equals(msg.CustomName) select temp;
                foreach (var d in ds)
                {
                    content += d.Time + string.Format("(记录者:{0})", d.ReceName) + Environment.NewLine + d.Content + Environment.NewLine + Environment.NewLine;
                }
                txtContent.Text = content;
            }else{
                txtContent.Text = "";
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int flag = AccessHelper.DeleteAllCustomMsg();
            if (flag > 0)
            {
                txtContent.Text = "";
                lbCustom.DataSource = null;
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            int day;
            if (int.TryParse(txtDay.Text, out day))
            {
                DateTime baseDateTime = DateTime.Now;
                baseDateTime.AddDays(day);
                if (dt != null)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        CustomMsg custom = new CustomMsg() { Id = int.Parse(row["Id"].ToString()), Content = row["Content"].ToString(), Time = row["WriterTime"].ToString(), CustomName = row["CustomName"].ToString(), ReceName = row["ReceName"].ToString() };
                        var ds = from temp in customs where temp.CustomName.Equals(custom.CustomName) && temp.DateTime.CompareTo(baseDateTime) >= 1 select temp;
                        if (ds.Count() <= 0)
                        {
                            customs.Add(custom);
                        }
                        msgs.Add(custom);
                    }
                    lbCustom.DataSource = customs;
                }
            }
        }
    }
}